/**
 * 语言切换管理模块
 * 提供全站语言切换功能
 */
const LanguageSwitch = (function() {
    // 语言配置
    const languages = {
        zh: {
            name: '中文',
            code: 'zh'
        },
        en: {
            name: 'English',
            code: 'en'
        }
    };

    // 网站各页面中英文内容映射
    const translations = {
        // 导航栏翻译
        navbar: {
            home: { zh: '首页', en: 'Home' },
            about: { zh: '关于我们', en: 'About Us' },
            products: { zh: '产品与解决方案', en: 'Products & Solutions' },
            cases: { zh: '成功案例', en: 'Cases' },
            news: { zh: '新闻资讯', en: 'News' },
            contact: { zh: '联系我们', en: 'Contact Us' },
            language: { zh: 'En/中', en: '中/En' }
        },
        
        // 首页翻译
        index: {
            heroTitle: { 
                zh: '智联未来，创造价值', 
                en: 'Intelligent Future, Creating Value' 
            },
            heroDesc: { 
                zh: '我们致力于通过创新的技术与解决方案，为全球客户构建更高效、更智能的商业世界。', 
                en: 'We are committed to building a more efficient and intelligent business world for global clients through innovative technologies and solutions.' 
            },
            heroBtn1: { 
                zh: '探索解决方案', 
                en: 'Explore Solutions' 
            },
            heroBtn2: { 
                zh: '联系我们', 
                en: 'Contact Us' 
            },
            valuesTitle: { 
                zh: '为何选择我们', 
                en: 'Why Choose Us' 
            },
            value1Title: { 
                zh: '创新引领', 
                en: 'Innovation Leadership' 
            },
            value1Desc: { 
                zh: '持续投入研发，以尖端科技驱动行业变革。', 
                en: 'Continuously invest in R&D to drive industry transformation with cutting-edge technology.' 
            },
            value2Title: { 
                zh: '专业专注', 
                en: 'Professional Focus' 
            },
            value2Desc: { 
                zh: '深耕行业数十年，深刻理解客户需求与市场趋势。', 
                en: 'Deep industry expertise with decades of experience, deeply understanding customer needs and market trends.' 
            },
            value3Title: { 
                zh: '全球服务', 
                en: 'Global Service' 
            },
            value3Desc: { 
                zh: '业务遍布五大洲，为客户提供本地化、即时响应的支持。', 
                en: 'Business spanning five continents, providing localized and immediate response support to customers.' 
            },
            ctaTitle: { 
                zh: '准备好开启下一段卓越旅程了吗？', 
                en: 'Ready to embark on the next excellent journey?' 
            },
            ctaDesc: { 
                zh: '让我们携手，将挑战转化为机遇。', 
                en: 'Let us work together to turn challenges into opportunities.' 
            }
        },
        
        // 关于我们页面翻译
        about: {
            heroTitle: { 
                zh: '智引未来，顺势而为', 
                en: 'Guide the Future, Follow the Trend' 
            },
            heroDesc: { 
                zh: '郑州顺为数字科技有限公司，致力于用前沿的人工智能技术，为企业和政府开启数字化与智能化的新篇章。', 
                en: 'Zhengzhou Shunwei Digital Technology Co., Ltd. is committed to using cutting-edge artificial intelligence technology to open a new chapter of digitalization and intelligence for enterprises and governments.' 
            },
            positionTitle: { 
                zh: '我们的核心定位', 
                en: 'Our Core Positioning' 
            },
            mission: { 
                zh: '我们的使命', 
                en: 'Our Mission' 
            },
            missionDesc: { 
                zh: '让AI成为普惠的生产力工具，通过软件算法的创新，解决复杂世界中的关键问题，助力客户实现降本增效与模式创新。', 
                en: 'Make AI a universal productivity tool, solve key problems in the complex world through software algorithm innovation, and help clients achieve cost reduction and efficiency improvement and model innovation.' 
            },
            vision: { 
                zh: '我们的愿景', 
                en: 'Our Vision' 
            },
            visionDesc: { 
                zh: '成为中原地区最具影响力的AI软件解决方案提供商，构建连接技术与产业的智能生态。', 
                en: 'Become the most influential AI software solution provider in the Central Plains region, building an intelligent ecosystem connecting technology and industry.' 
            },
            capabilitiesTitle: { 
                zh: '我们的核心能力', 
                en: 'Our Core Capabilities' 
            },
            capability1Title: { 
                zh: '算法创新与研发', 
                en: 'Algorithm Innovation and R&D' 
            },
            capability1Desc: { 
                zh: '专注于机器学习、自然语言处理(NLP)与计算机视觉(CV)领域的核心算法研究与落地应用。', 
                en: 'Focus on core algorithm research and practical applications in machine learning, natural language processing (NLP), and computer vision (CV).' 
            },
            capability2Title: { 
                zh: '端到端解决方案', 
                en: 'End-to-End Solutions' 
            },
            capability2Desc: { 
                zh: '从数据分析、模型训练到系统部署与运维，提供全生命周期的AI软件产品与定制化解决方案。', 
                en: 'Provide full lifecycle AI software products and customized solutions from data analysis and model training to system deployment and operation.' 
            },
            capability3Title: { 
                zh: '行业深度融合', 
                en: 'Deep Industry Integration' 
            },
            capability3Desc: { 
                zh: '深耕智能政务、工业物联网、智慧金融、数字医疗等领域，打造场景化的智能应用。', 
                en: 'Deeply cultivate fields such as smart government, industrial IoT, intelligent finance, and digital healthcare to create scenario-based intelligent applications.' 
            },
            timelineTitle: { 
                zh: '我们的发展历程', 
                en: 'Our Development Journey' 
            },
            teamCultureTitle: { 
                zh: '团队与文化', 
                en: 'Team and Culture' 
            },
            teamTitle: { 
                zh: '我们的团队', 
                en: 'Our Team' 
            },
            teamDesc: { 
                zh: '我们汇聚了一批来自国内外顶尖院校的科学家、工程师和行业专家。我们坚信，是人才与创造力推动了技术的边界。', 
                en: 'We have gathered a group of scientists, engineers, and industry experts from top domestic and international institutions. We believe that talent and creativity push the boundaries of technology.' 
            },
            cultureTitle: { 
                zh: '文化价值观', 
                en: 'Cultural Values' 
            },
            value1Title: { 
                zh: '极致创新', 
                en: 'Extreme Innovation' 
            },
            value1Desc: { 
                zh: '永不满足于现状，追求技术上的突破。', 
                en: 'Never satisfied with the status quo, pursuing breakthroughs in technology.' 
            },
            value2Title: { 
                zh: '客户成功', 
                en: 'Customer Success' 
            },
            value2Desc: { 
                zh: '将客户的价值实现作为我们成功的最终标准。', 
                en: 'Taking the realization of customer value as our ultimate standard of success.' 
            },
            value3Title: { 
                zh: '协同共进', 
                en: 'Collaborative Progress' 
            },
            value3Desc: { 
                zh: '开放透明，共享知识，共同成长。', 
                en: 'Open and transparent, sharing knowledge, growing together.' 
            },
            ctaTitle: { 
                zh: '未来已来，与您同行', 
                en: 'The Future is Here, Walking with You' 
            },
            ctaDesc: { 
                zh: '期待与您携手，共同探索人工智能的无限可能。', 
                en: 'Looking forward to exploring the infinite possibilities of artificial intelligence with you.' 
            }
        },
        
        // 产品与解决方案页面翻译
        product: {
            heroTitle: { 
                zh: '智能驱动，重塑行业核心体验', 
                en: 'Intelligent Drive, Reshaping Core Industry Experience' 
            },
            heroDesc: { 
                zh: '将前沿大语言模型（LLM）技术与行业深度结合，提供可信赖的AI决策助手。', 
                en: 'Combining cutting-edge Large Language Model (LLM) technology with industry expertise to provide reliable AI decision assistants.' 
            },
            product1Name: { 
                zh: '小智医疗 - 您的AI健康伙伴', 
                en: 'XiaoZhi Medical - Your AI Health Partner' 
            },
            product1Desc: { 
                zh: '精准洞察，高效诊疗，让健康管理更智能', 
                en: 'Accurate insights, efficient diagnosis and treatment, making health management smarter' 
            },
            product2Name: { 
                zh: '志愿填报大师 - 新高考下的智能引路人', 
                en: 'Volunteer Master - Intelligent Guide for New College Entrance Exam' 
            },
            product2Desc: { 
                zh: '数据驱动，规避风险，为未来做出最优选择', 
                en: 'Data-driven, risk-averse, making optimal choices for the future' 
            },
            techTitle: { 
                zh: '我们选择的技术栈，只为确保稳定与高效', 
                en: 'Our Technology Stack, Chosen for Stability and Efficiency' 
            },
            tech1Title: { 
                zh: '企业级开发', 
                en: 'Enterprise Development' 
            },
            tech1Desc: { 
                zh: '基于Java技术生态，确保应用的高并发、高稳定与易维护。', 
                en: 'Based on the Java technology ecosystem, ensuring high concurrency, stability, and maintainability of applications.' 
            },
            tech2Title: { 
                zh: 'AI原生设计', 
                en: 'AI-Native Design' 
            },
            tech2Desc: { 
                zh: '深度运用LangChain4j，打造专业级AI智能体（Agent），而非简单聊天机器人。', 
                en: 'Deep use of LangChain4j to create professional AI agents, not simple chatbots.' 
            },
            tech3Title: { 
                zh: '数据安全', 
                en: 'Data Security' 
            },
            tech3Desc: { 
                zh: '私有化部署支持，所有数据可留存于客户本地，安全无忧。', 
                en: 'Supports private deployment, with all data retained locally at the customer site, safe and worry-free.' 
            },
            ctaTitle: { 
                zh: '未来已来，与您同行', 
                en: 'The Future is Here, Walking with You' 
            },
            ctaDesc: { 
                zh: '期待与您携手，共同探索人工智能的无限可能。', 
                en: 'Looking forward to exploring the infinite possibilities of artificial intelligence with you.' 
            }
        },
        
        // 联系我们页面翻译
        contact: {
            title: { 
                zh: '联系我们', 
                en: 'Contact Us' 
            },
            subtitle: { 
                zh: '我们期待与您建立联系，共同探讨合作机会', 
                en: 'We look forward to establishing contact with you and exploring cooperation opportunities' 
            },
            companyNameLabel: { 
                zh: '公司名称：', 
                en: 'Company Name:' 
            },
            companyName: { 
                zh: '郑州顺为数字科技有限公司', 
                en: 'Zhengzhou Shunwei Digital Technology Co., Ltd.' 
            },
            companyAddressLabel: { 
                zh: '公司地址：', 
                en: 'Company Address:' 
            },
            companyAddress: { 
                zh: '郑州市高新区莲花街威科姆大厦', 
                en: 'Weikom Building, Lianhua Street, High-tech Zone, Zhengzhou City' 
            },
            contactPersonLabel: { 
                zh: '联系人：', 
                en: 'Contact Person:' 
            },
            contactPerson: { 
                zh: '陈先生', 
                en: 'Mr. Chen' 
            },
            phoneLabel: { 
                zh: '联系电话：', 
                en: 'Phone:' 
            },
            phone: { 
                zh: '1873991875', 
                en: '1873991875' 
            },
            emailLabel: { 
                zh: '邮箱：', 
                en: 'Email:' 
            },
            email: { 
                zh: 'kk@163.com', 
                en: 'kk@163.com' 
            },
            companyInfoTitle: { 
                zh: '公司信息', 
                en: 'Company Information' 
            },
            phoneTitle: { 
                zh: '电话联系', 
                en: 'Phone Contact' 
            },
            emailTitle: { 
                zh: '邮件联系', 
                en: 'Email Contact' 
            },
            addressTitle: { 
                zh: '办公地址', 
                en: 'Office Address' 
            },
            recruitmentTitle: { 
                zh: '招聘英才', 
                en: 'Join Our Team' 
            },
            position1Title: { 
                zh: '前端Web开发工程师', 
                en: 'Frontend Web Developer' 
            },
            position1Req1: { 
                zh: '熟练掌握HTML/CSS/JavaScript', 
                en: 'Proficient in HTML/CSS/JavaScript' 
            },
            position1Req2: { 
                zh: '熟悉Vue/React等主流框架', 
                en: 'Familiar with mainstream frameworks like Vue/React' 
            },
            position1Req3: { 
                zh: '有响应式开发经验', 
                en: 'Experience with responsive development' 
            },
            position1Req4: { 
                zh: '本科及以上学历，2年半以上经验', 
                en: 'Bachelor’s degree or above, 2.5+ years of experience'
            },
            position2Title: { 
                zh: '后端Java开发工程师', 
                en: 'Backend Java Developer' 
            },
            position2Req1: { 
                zh: '熟练掌握Java及Spring框架', 
                en: 'Proficient in Java and Spring framework' 
            },
            position2Req2: { 
                zh: '熟悉MySQL/Redis等数据库', 
                en: 'Familiar with databases like MySQL/Redis' 
            },
            position2Req3: { 
                zh: '了解微服务架构', 
                en: 'Understanding of microservices architecture' 
            },
            position2Req4: { 
                zh: '本科及以上学历，3年以上经验', 
                en: 'Bachelor’s degree or above, 3+ years of experience'
            },
            position3Title: { 
                zh: '人工智能LLM应用开发工程师', 
                en: "AI LLMs App's Developer"
            },
            position3Req1: { 
                zh: '熟悉LangChain等AI框架', 
                en: 'Familiar with AI frameworks like LangChain' 
            },
            position3Req2: { 
                zh: '掌握Python/Java等编程语言', 
                en: 'Proficient in programming languages like Python/Java' 
            },
            position3Req3: { 
                zh: '了解大模型原理与应用', 
                en: 'Understanding of large model principles and applications' 
            },
            position3Req4: { 
                zh: '本科及以上学历，有相关经验优先', 
                en: 'Bachelor’s degree or above, relevant experience preferred'
            },
            requirementsTitle: { 
                zh: '岗位要求', 
                en: 'Requirements' 
            },
            ctaTitle: { 
                zh: '未来已来，与您同行', 
                en: 'The Future is Here, Walking with You' 
            },
            ctaDesc: { 
                zh: '期待与您携手，共同探索人工智能的无限可能。', 
                en: 'Looking forward to exploring the infinite possibilities of artificial intelligence with you.' 
            }
        }
    };

    // 获取当前语言设置
    function getCurrentLanguage() {
        return localStorage.getItem('language') || 'zh';
    }

    // 设置当前语言
    function setCurrentLanguage(lang) {
        if (languages[lang]) {
            localStorage.setItem('language', lang);
            return true;
        }
        return false;
    }

    // 获取翻译文本
    function getTranslation(key, page) {
        const currentLang = getCurrentLanguage();
        
        // 如果指定了页面，则从该页面的翻译中查找
        if (page && translations[page] && translations[page][key]) {
            return translations[page][key][currentLang] || key;
        }
        
        // 否则在所有页面中查找
        for (let p in translations) {
            if (translations[p][key]) {
                return translations[p][key][currentLang] || key;
            }
        }
        
        return key;
    }

    // 应用翻译到页面
    function applyTranslations() {
        const currentLang = getCurrentLanguage();
        
        // 翻译所有带有data-i18n属性的元素
        const elements = document.querySelectorAll('[data-i18n]');
        elements.forEach(element => {
            const key = element.getAttribute('data-i18n');
            const page = element.getAttribute('data-i18n-page');
            element.textContent = getTranslation(key, page);
        });
        
        // 更新语言切换按钮显示
        const langSwitch = document.querySelector('.language-switch');
        if (langSwitch) {
            langSwitch.textContent = currentLang === 'zh' ? '中/En' : 'En/中';
        }
        
        // 更新页面语言属性
        document.documentElement.lang = currentLang === 'zh' ? 'zh-CN' : 'en';
    }

    // 切换语言
    function switchLanguage() {
        const currentLang = getCurrentLanguage();
        const newLang = currentLang === 'zh' ? 'en' : 'zh';
        
        setCurrentLanguage(newLang);
        applyTranslations();
        
        // 触发自定义事件，便于其他模块监听语言切换
        window.dispatchEvent(new CustomEvent('languageChanged', { 
            detail: { language: newLang } 
        }));
    }

    // 初始化语言切换功能
    function init() {
        // 等待DOM加载完成
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', applyTranslations);
        } else {
            applyTranslations();
        }
        
        // 绑定语言切换按钮事件
        document.addEventListener('click', function(e) {
            if (e.target.classList.contains('language-switch')) {
                switchLanguage();
                e.preventDefault();
            }
        });
    }

    // 公共接口
    return {
        init: init,
        getCurrentLanguage: getCurrentLanguage,
        switchLanguage: switchLanguage,
        getTranslation: getTranslation,
        applyTranslations: applyTranslations
    };
})();

// 初始化语言切换功能
LanguageSwitch.init();